package com.gears_software.sxdc.dccx.ui.util.popu;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.sharedPreference.DCCXSp;
import com.gears_software.sxdc.dccx.data.sharedPreference.DCCXSp_;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.App;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.sharedpreferences.Pref;

import razerdp.basepopup.BasePopupWindow;

/**
 * Created by chezhenzhen on 2017/7/22.
 */
public class SortPopup extends BasePopupWindow implements View.OnClickListener {


    RelativeLayout re_jiage;
    RelativeLayout re_shijian;
    TextView txt_dui_icon;
    TextView txt_dui_icon1;
    TextView txt_jiage;
    TextView txt_shijian;
    Context context;
    DCCXSp_ sp;
    boolean isTime = false;
    boolean isPrice = false;
    /**
     * 初始化接口变量
     */
    SortcallBack sortcallBack = null;

    public SortPopup(Activity context) {
        super(context);
        this.context = context;
        sp = new DCCXSp_(context);
        txt_dui_icon = (TextView) findViewById(R.id.txt_dui_icon);
        txt_dui_icon1 = (TextView) findViewById(R.id.txt_dui_icon1);
        txt_jiage = (TextView) findViewById(R.id.txt_jiage);
        txt_shijian = (TextView) findViewById(R.id.txt_shijian);
        re_jiage = (RelativeLayout) findViewById(R.id.re_jiage);
        re_shijian = (RelativeLayout) findViewById(R.id.re_shijian);
        txt_dui_icon.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        txt_dui_icon1.setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));
        isTime = sp.isTimeSort().get();
        isPrice = sp.isPriceSort().get();
        if (isTime) {
            sp.sortTag().put("时间");
        }
        if (isPrice) {
            sp.sortTag().put("价格");
        }
        if (sp.sortTag().get().equals("时间")){
            txt_shijian.setTextColor(ContextCompat.getColor(context, R.color.colorFontOrange));
            txt_dui_icon1.setTextColor(ContextCompat.getColor(context, R.color.colorFontOrange));
        }else if (sp.sortTag().get().equals("价格")){
            txt_jiage.setTextColor(ContextCompat.getColor(context, R.color.colorFontOrange));
            txt_dui_icon.setTextColor(ContextCompat.getColor(context, R.color.colorFontOrange));
        }

        setViewClickListener(this, re_jiage, re_shijian);

    }

    @Override
    protected Animation initShowAnimation() {
//        AnimationSet set = new AnimationSet(true);
//        set.setInterpolator(new DecelerateInterpolator());
//        set.addAnimation(getScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0));
//        set.addAnimation(getDefaultAlphaAnimation());
        return getDefaultScaleAnimation();
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    @Override
    public View onCreatePopupView() {
        return createPopupById(R.layout.popup_sort);
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.popup_anima);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.re_jiage:
                normal();
                txt_jiage.setTextColor(ContextCompat.getColor(context, R.color.colorFontOrange));
                txt_dui_icon.setTextColor(ContextCompat.getColor(context, R.color.colorFontOrange));
                isPrice = !isPrice;
                sp.isPriceSort().put(isPrice);
                sp.isTimeSort().put(false);
                sp.sortTag().put("价格");
                sortcallBack.sortClick("价格");
                dismissWithOutAnima();
                break;
            case R.id.re_shijian:
                normal();
                txt_shijian.setTextColor(ContextCompat.getColor(context, R.color.colorFontOrange));
                txt_dui_icon1.setTextColor(ContextCompat.getColor(context, R.color.colorFontOrange));
                isTime = !isTime;
                sp.isTimeSort().put(isTime);
                sp.isPriceSort().put(false);
                sp.sortTag().put("时间");
                sortcallBack.sortClick("时间");
                dismissWithOutAnima();
                break;
            default:
                break;
        }

    }

    private void normal() {
        txt_jiage.setTextColor(ContextCompat.getColor(context, R.color.colorFontBigGray));
        txt_shijian.setTextColor(ContextCompat.getColor(context, R.color.colorFontBigGray));
        txt_dui_icon.setTextColor(ContextCompat.getColor(context, R.color.colorFontBigGray));
        txt_dui_icon1.setTextColor(ContextCompat.getColor(context, R.color.colorFontBigGray));
    }

    /**
     * 自定义控件的自定义事件
     * @param sortcallBack 接口类型
     */
    public void setSortClick(SortcallBack sortcallBack) {
        this.sortcallBack = sortcallBack;
    }

    //自定义一个接口
    public interface SortcallBack {
        public void sortClick(String sortTag);
    }

}
